heartbeat与lvs结合来给lvs 添加健康监测

heartbeat与lvs结合来给lvs 添加健康监测

简述

作用:之前我们只是单纯的部署了lvs,实现了负载均衡,但是lvs有一个缺点,假如说RS中的其中一个down掉了,那么访问就会出现异常,lvs只是使用算法去调度这些RS,但是并没有检测RS是否健康的功能,所以我们要请心跳HA来帮忙,让我们的部署更加严谨,让我们一起开始部署吧!

配置heartbeat里的ldirectord(调度端serevr1)

调度端:
server1 172.25.9.1
Realserver:
server2 172.25.9.2
server3 172.25.9.3
1.安装ldirectord

1
2
3
[root@server1 heartbeat]# yum install ldirectord-3.9.5-3.1.x86_64.rpm 
[root@server1 ha.d]# cd /usr/share/doc/ldirectord-3.9.5/
[root@server1 ldirectord-3.9.5]# cp ldirectord.cf /etc/ha.d/

2.修改配置文件(这个文件会帮我们自动生成arp规则)

1
[root@server1 ha.d]# vim ldirectord.cf

做如下修改:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Sample for an http virtual service
virtual=172.25.9.100:80
real=172.25.9.2:80 gate
real=172.25.9.3:80 gate
fallback=127.0.0.1:80 gate #如果RS端都挂掉的话,调度器会顶上
service=http
scheduler=rr
#persistent=600
#netmask=255.255.255.255
protocol=tcp
checktype=negotiate
checkport=80
request="index.html"
# receive="Test Page"
# virtualhost=www.x.y.z

3.查看arp规则:

1
2
3
4
[root@server1 ha.d]# ipvsadm -l
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn

4.启动ldirectord服务并查看规则:

1
2
3
4
5
6
7
8
[root@server1 ha.d]# /etc/init.d/ldirectord start
[root@server1 ha.d]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.25.9.100:80 rr
-> 172.25.9.2:80 Route 1 0 0
-> 172.25.9.3:80 Route 1 0 0

实验结果

1.直接测试:

1
2
3
4
[root@foundation187 ~]# links -dump 172.25.9.100
hello server2
[root@foundation187 ~]# links -dump 172.25.9.100
hello server3

2.若现在把其中一台RS停掉

1
[root@server2 network-scripts]# /etc/init.d/httpd stop

1
2
3
4
[root@foundation187 ~]# links -dump 172.25.9.100
hello server3
[root@foundation187 ~]# links -dump 172.25.9.100
hello server3

若现在把所有的RS的httpd服务停掉

1
[root@server1 heartbeat]# /etc/init.d/httpd stop

1
2
3
4
[root@foundation187 ~]# links -dump 172.25.9.100
hello server1
[root@foundation187 ~]# links -dump 172.25.9.100
hello server1

把HA与lvs结合

HA的集群:
server1 172.25.9.1 server2 172.25.9.2
Realserver:
server3 172.25.9.3 server4 172.25.9.4
【注】:此处的server1与server2之前做过HA,请参考文档“HEARTBEAT(心跳)”

调度端的操作(server1&server2)

1.关闭server1上的httpd服务和ldirectord服务,等会集群会帮助我们启动他们

1
2
3
4
5
[root@server1 heartbeat]# /etc/init.d/httpd stop
[root@server1 heartbeat]# /etc/init.d/ldirectord stop
[root@server1 heartbeat]# chkconfig ipvsadm --list
ipvsadm 0:off 1:off 2:off 3:off 4:off 5:off 6:off
[root@server1 heartbeat]# chkconfig ldirectord off

2.关闭server2的HA服务,安装ldirectord

1
2
3
[root@server2 ~]# chkconfig heartbeat off
[root@server2 ~]# /etc/init.d/heartbeat stop
[root@server2 heartbeat]# yum install ldirectord-3.9.5-3.1.x86_64.rpm

3.修改server1上的HA配置文件,并发送给server2,两者做相同配置
haresource文件:

1
2
3
4
5
[root@server1 ha.d]# pwd
/etc/ha.d
[root@server1 ha.d]# vim haresources
在文件中加入:
server1.example.com IPaddr::172.25.9.100/24/eth1 ldirectord httpd

ldirectord文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@server1 ha.d]# vim ldirectord.cf
做如下修改:
# Sample for an http virtual service
virtual=172.25.9.100:80
real=172.25.9.3:80 gate
real=172.25.9.4:80 gate
fallback=127.0.0.1:80 gate
service=http
scheduler=rr
#persistent=600
#netmask=255.255.255.255
protocol=tcp
checktype=negotiate
checkport=80
request="index.html"

1
[root@server1 ha.d]# scp ldirectord.cf haresources 172.25.9.2:/etc/ha.d/

【注】:/etc/ha.d里面的另外两个文件与之前配置HA文件相同,不做修改

RS端(serevr3&server4)

1
2
3
4
5
[root@server3 ~]# cat /etc/sysctl.conf 
.....
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
[root@server4 ~]# ifconfig lo:0 172.25.9.100 netmask 255.255.255.255

实验结果

1
2
3
4
5
6
7
8
9
10
11
[root@server2 ha.d]# /etc/init.d/heartbeat start
Starting High-Availability services: INFO: Resource is stopped
Done.

[root@server2 ha.d]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.25.9.100:80 rr
-> 172.25.9.3:80 Route 1 0 0
-> 172.25.9.4:80 Route 1 0 0

当server1与server2中任何一个down掉,另外一个会自动的顶上。(HA)
server3与server4之间实现负载均衡